package com.example.horizontalrefresh.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import androidx.annotation.Nullable;
import androidx.core.view.MotionEventCompat;
import androidx.core.view.ViewConfigurationCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @ProjectName: zhcg6
 * @Package: com.tfrd.city.view
 * @ClassName: BetterRecyclerView
 * @Description:
 * @Author: 作者名：zhy
 * @CreateDate: 2019/8/20 17:54
 */
public class BetterRecyclerView extends RecyclerView {
    private int touchSlop;
    private Context mContext;
    private int INVALID_POINTER = -1;
    private int scrollPointerId = INVALID_POINTER;

    private int initialTouchX;
    private int initialTouchY;


    private final static String TAG = "BetterRecyclerView";

    public BetterRecyclerView(Context context) {
//        super(context);
        this(context, null);
    }

    public BetterRecyclerView(Context context, @Nullable AttributeSet attrs) {
//        super(context, attrs);
        this(context, attrs, 0);
    }

    public BetterRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        ViewConfiguration vc = ViewConfiguration.get(context);
        touchSlop =vc.getScaledEdgeSlop() ;
        mContext =context ;

    }

    @Override
    public void setScrollingTouchSlop(int slopConstant) {
        super.setScrollingTouchSlop(slopConstant);
        ViewConfiguration vc = ViewConfiguration.get(mContext);
        switch (slopConstant) {
            case TOUCH_SLOP_DEFAULT:
                touchSlop = vc.getScaledTouchSlop();
                break;
            case TOUCH_SLOP_PAGING:
                touchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(vc);
                break;

        }

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {

        if (e==null){
            return  false ;
        }
        int action = MotionEventCompat.getActionMasked(e);
        int actionIndex = MotionEventCompat.getActionIndex(e);

        switch (action){
            case MotionEvent.ACTION_DOWN :
                scrollPointerId = MotionEventCompat.getPointerId(e, 0);
                initialTouchX = Math.round(e.getX() + 0.5f);
                initialTouchY = Math.round(e.getY() + 0.5f);

                return super.onInterceptTouchEvent(e);

            case MotionEvent.ACTION_POINTER_DOWN:

                scrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
                initialTouchX = Math.round(MotionEventCompat.getX(e, actionIndex) + 0.5f);
                initialTouchY = Math.round(MotionEventCompat.getY(e, actionIndex) + 0.5f);

                return super.onInterceptTouchEvent(e);
            case MotionEvent.ACTION_MOVE:
                final int index =e.findPointerIndex(scrollPointerId);
                if ( index<0){
                    return  false ;
                }

             int x = Math.round(MotionEventCompat.getX(e, index) + 0.5f);
                int y = Math.round(MotionEventCompat.getY(e,index)+0.5f);

                if (getScrollState()!=SCROLL_STATE_DRAGGING){

                  int dx =x-initialTouchX ;

                    int  dy = y-initialTouchY ;
                    boolean startScroll = false;
                    if ((getLayoutManager().canScrollHorizontally()&&Math.abs(dx)>touchSlop)&&(getLayoutManager().canScrollVertically()||Math.abs(dx)>Math.abs((dy)))){
                        startScroll = true ;
                    }

                   /* if ((getLayoutManager().canScrollHorizontally()&&Math.abs(dy)>touchSlop)*//*&&(getLayoutManager().canScrollHorizontally()||Math.abs(dy)>Math.abs((dx)))*//*){

                        startScroll = true ;

                    }*/
                    return  startScroll ;
                }
                return false;
        }
        return false;
    }

}
